package com.dachuang.canjiren.service.impl;

import com.dachuang.canjiren.entity.Banner;
import com.dachuang.canjiren.entity.Business;
import com.dachuang.canjiren.entity.User;
import com.dachuang.canjiren.entity.VO.CommentVO;
import com.dachuang.canjiren.mapper.BusinessMapper;
import com.dachuang.canjiren.mapper.UserMapper;
import com.dachuang.canjiren.service.BusinessService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dachuang.common.exception.BizCodeEnume;
import com.dachuang.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-27
 */
@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {

    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public R insertBusiness(String logo, String name, String address, String firld, String content, String prospectus, Integer userId) {
        if (Objects.isNull(userId)){
            return R.error(BizCodeEnume.ERROR20006);
        }
        if (StringUtils.isEmpty(logo)){
            return R.error(BizCodeEnume.ERROR60001);
        }
        if (StringUtils.isEmpty(name)){
            return R.error(BizCodeEnume.ERROR50001);
        }
        if ( StringUtils.isEmpty(address)){
            return R.error(BizCodeEnume.ERROR60002);
        }
        if (StringUtils.isEmpty(firld) ){
            return R.error(BizCodeEnume.ERROR60003);
        }
        if (StringUtils.isEmpty(content)){
            return R.error(BizCodeEnume.ERROR60004);
        }
        if (StringUtils.isEmpty(prospectus)){
            return R.error(BizCodeEnume.ERROR60005);
        }

        //验证userId
        User user = userMapper.selectById(userId);
        if (Objects.isNull(user)){
            return R.error(BizCodeEnume.ERROR20004);
        }

        Business business = new Business();
        business.setLogo(logo);
        business.setName(name);
        business.setAddress(address);
        business.setFirld(firld);
        business.setContent(content);
        business.setProspectus(prospectus);
        business.setUserId(userId);

        if (businessMapper.insertBusiness(business) > 0){
            return R.ok();
        }
        return R.error();
    }

    @Override
    public R selectBusiness(Integer id) {
        if (Objects.isNull(id)){
            return R.error(BizCodeEnume.ERROR20005);
        }
        Business business = businessMapper.selectBusiness(id);
        if (Objects.isNull(business)){
            return R.error(BizCodeEnume.ERROR40001);
        }
        R r = R.ok();
        r.put("data",business);
        return r;
    }

    @Override
    public R selectUserBusiness(Integer userId) {
            if (Objects.isNull(userId)){
                return R.error(BizCodeEnume.ERROR20005);
            }
        List<Business> business = businessMapper.selectUserBusiness(userId);
            if (Objects.isNull(business)){
                return R.error(BizCodeEnume.ERROR40001);
            }
            R r = R.ok();
            r.put("data",business);
            return r;
    }

    @Override
    public R selectBusinessList() {
        List<Business> list = businessMapper.selectBusinessList();
        Map<String,Object> map = new HashMap<>();
        if (list.size() > 0){
            map.put("data",list);
            return R.ok(map);
        }
        return R.error();
    }
}
